<?php 
try {
	include_once('db/constants.php');
	include_once('db/get_contents_db.php');
	include_once('db/get_categories_db.php');
	include_once('db/general_functions_db.php');
	include_once('db/error_log_db.php');
	include_once('facebook_login.php');

	$current_file = "uploadContent";
	include_once("users_flow_logger.php");

	$category_id = isset($_REQUEST['category_id']) ? $_REQUEST['category_id'] : -1;

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow-x: hidden">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo HTML_TITLE;?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
	<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
	
	<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
	<script src="js/spin.js" type="text/javascript"></script>
	<script src="js/wrap_content.js?<?php echo REVISION;?>" type="text/javascript"></script>

	<script>
		var allCategories = JSON.parse( '<?php echo get_all_categories_names($user_profile['id']);?>' );
		var searchBoxCleaned = false;
		var categoryIdSelected = null;
		var submitting = false;
		var currentContent = null;
		var currentCategory = null;
		<?php
			$category_json = isset($_REQUEST['category_id']) ? get_one_category($_REQUEST['category_id'], $user_profile['id']) : null;
			$category = ($category_json != "") ? json_decode($category_json) : ""; 

			if ( isset($_REQUEST['content_id']) ) { 
				echo "currentContent = JSON.parse(\"".addslashes(get_one_content($_REQUEST['content_id'], $user_profile['id']))."\");";
			}

			if ( isset($_REQUEST['category_id']) ) { 
				echo "currentCategory = JSON.parse(\"".addslashes($category_json)."\");";
			}
		?>


		function submitContent(){
			if (submitting == true)
				return;

			if (categoryIdSelected == null) {
				alert("<?php echo NEED_SET_CATEGORY;?>");
				return;
			}

			submitting = true;
			var content = capitaliseFirstLetter(document.getElementById("content_textarea").value.replace(/\n/g, "<br />"));
			
			spinner.spin(spinnerTarget);

			var callUrl = "db/upload_content_db.php";
			callUrl = callUrl + "?user_id=<?php echo $user_profile['id']; ?>";
			callUrl = callUrl + "&content=" + content;
			callUrl = callUrl + "&first_name=<?php echo urlencode($user_profile['first_name']); ?>";
			callUrl = callUrl + "&last_name=<?php echo urlencode($user_profile['last_name']); ?>";
			callUrl = callUrl + "&category_id=" + categoryIdSelected;
			callUrl = callUrl + "&is_anonymous=" + document.getElementById('hide_name').checked;

			if (currentContent != null) {
				callUrl = callUrl + "&content_id=<?php if ( isset($_REQUEST['content_id']) ) echo $_REQUEST['content_id']; ?>";
			}

			$.post(callUrl, {
			}, function(response){
				window.location.href="<?php echo CONTENTS_URL;?>?content_id=" + response + "&category_id=<?php echo $category_id;?>";                             
			});

		}

		function capitaliseFirstLetter(string) {
		    return string.charAt(0).toUpperCase() + string.slice(1);
		}

		function categorySuggest(str) {
			categoryIdSelected = null;

			if (str == "") {
				document.getElementById("category-search-suggest").style.visibility = "hidden";
				return;
			}

			var optionsAdded = 0;
			var MAX_OPTIONS = 5;
			var suggestedValues = "";


			for (var i = 0; i < allCategories.length; i++) {
				var currentOption = allCategories[i];
				var catName = removeAccents(currentOption.name.toLowerCase());
				var inputValue = removeAccents(str.toLowerCase());

				if ( catName.indexOf( inputValue ) == 0) {
					suggestedValues = suggestedValues + '<div id="search-option" onclick="selectCategory(' + currentOption.category_id + ', \'' + currentOption.name + '\');">' + currentOption.name + "</div>";
					optionsAdded++;
				}

				if ( catName == inputValue) {
					categoryIdSelected = currentOption.category_id;
					document.getElementById("category-search").value = currentOption.name;
					document.getElementById("category-search-suggest").style.visibility = "hidden";
					return;
				}

				if (optionsAdded >= MAX_OPTIONS)
					break;
			}

			suggestedValues = suggestedValues + '<hr/>'; 
			suggestedValues = suggestedValues + '<a id="search-option" style="padding-right: 153px; text-decoration: none; color: #6d6d6d;" href="<?php echo UPLOAD_CATEGORY;?>"> <?php echo CREATE_NEW_CATEGORY; ?> </a>'; 

			document.getElementById("category-search-suggest").innerHTML = suggestedValues;
			document.getElementById("category-search-suggest").style.visibility = "visible";
		}

		function selectCategory(category_id, category_name) {
			document.getElementById("category-search").value = category_name;
			document.getElementById("category-search-suggest").style.visibility = "hidden";

			categoryIdSelected = category_id;
		}

		function cleanSearchBox() {
			if (searchBoxCleaned == false) {
				document.getElementById("category-search").value = "";
				searchBoxCleaned = true;
			}

		}

		function removeAccents(str) {
			var retval = str.replace(/á/g, "a");
			retval = retval.replace(/é/g, "e");
			retval = retval.replace(/í/g, "i");
			retval = retval.replace(/ó/g, "o");
			retval = retval.replace(/ú/g, "u");

			return retval;
		}

	</script>
	
</head>
<body>
	
	<div id="fb-root"></div>
	<script>(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/es_LA/all.js";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));</script>


    <script>		
	window.fbAsyncInit = function() {
	FB.init({
	  appId : '<?php echo APP_ID;?>',
	  xfbml : true,
	  version : 'v2.0'
	});
	};
	(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/es_LA/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
    </script>

<?php 
$headerFirstOption = SEE_MY_PUBLICATIONS;
$headerSecondOption = CATEGORIES;
$headerThirdOption = HOME;

$headerFirstUrl = PUBLISH_CONTENT."?coming_from=".$current_file;
$headerSecondUrl = CATEGORIES_URL."?coming_from=".$current_file;
$headerThirdUrl = INDEX."?coming_from=".$current_file;

$headerFirstUrl = ($category != "") ? $headerFirstUrl."&category_id=".$category_id : $headerFirstUrl;
$headerSecondUrl = ($category != "") ? $headerSecondUrl."&category_id=".$category_id : $headerSecondUrl;


$headerFirstClass = "button";
$headerSecondClass = "button-highlighted-green";
$headerThirdClass = "button";

include_once("header_redirects.php"); 
include_once("header.php"); 
?>

<!-- Slider -->
<div id="slider">
	<div style="font-size: 25px; text-align: center; margin-top: 0px; padding-top: 17px; margin-bottom: -19px; color: #E8E8E8;">
		<input id="category-search" type="text" value="<?php echo WRITE_CATEGORY_HERE;?>" style="font-size: 25px; width:400px;" onclick="cleanSearchBox();" onkeyup="categorySuggest(this.value)">
		<div id="category-search-suggest" style="visibility:hidden; width: 402px; position: absolute; left: 50%; margin-left: -202px; margin-top: 5px; z-index: 1; background: white; text-align: left; color: #6d6d6d; border: 1px solid #A5ACB2; line-height: 32px;"></div>
	</div>
	<div class="shell" style="margin-top: 20px;">
		
		<div class="slider-holder">
			<div class="slides" style="top: 0px; padding-bottom: 50px;">
				<div align="center" style="color:#ffffff;">
					&nbsp;
				</div>

			    	<div class="slide-bg">
				    	<table border="0px" style="width: 94%;" class="content-table">
				    		<tr>
				    			<td colspan="2" class="publish-content-td" valign="top">
				    				<textarea id="content_textarea" style="height: 160px;" tabindex="1"></textarea>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td colspan="2"> </td>
				    		<tr>
				    		<tr style="height: 14%;">
				    			<td width="600px" style="font-size: 14px;">
				    				<input type="checkbox" id="hide_name" class="hide-name-checkbox"><?php echo HIDE_NAME;?><br>
				    			</td>
								<td align="right" valign="bottom"> <a class="button" onclick="submitContent()"><?php echo PUBLISH; ?></a> </td>
				    		</tr>
				    	</table>
			    	</div>
			    	<span class="slide-shadow">&nbsp;</span>

			</div>
			
			<!-- End Slider Navigation -->
		</div>
		
	</div>
</div>
<!-- End Slider -->

<?php include_once('downOptions.php');?>

<script>
	var opts = {
		top: '30%', // Top position relative to parent
		left: '50%' // Left position relative to parent
	};
	var spinnerTarget = document.getElementById('slider');
	var spinner = new Spinner(opts).spin(spinnerTarget);
	spinner.stop();

	if (currentContent != null) {
		document.getElementById("content_textarea").value = replaceAll("<br />", '\n', currentContent.content);
		document.getElementById("hide_name").checked = (currentContent.is_anonymous == "1") ? true : false;
	}

	if (currentCategory != null) {
		searchBoxCleaned = true;
		categoryIdSelected = currentCategory.category_id;
		document.getElementById("category-search").value = currentCategory.name;
	}

	function replaceAll(find, replace, str) {
		return str.replace(new RegExp(find, 'g'), replace);
	}
</script>

</body>
</html>
